{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Sum of Unique Elements"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #hash-table #counting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #哈希表 #计数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: sumOfUnique"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #唯一元素的和"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个整数数组 <code>nums</code> 。数组中唯一元素是那些只出现 <strong>恰好一次</strong> 的元素。</p>\n",
    "\n",
    "<p>请你返回 <code>nums</code> 中唯一元素的 <strong>和</strong> 。</p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>nums = [1,2,3,2]\n",
    "<b>输出：</b>4\n",
    "<b>解释：</b>唯一元素为 [1,3] ，和为 4 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>nums = [1,1,1,1,1]\n",
    "<b>输出：</b>0\n",
    "<b>解释：</b>没有唯一元素，和为 0 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3 ：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>nums = [1,2,3,4,5]\n",
    "<b>输出：</b>15\n",
    "<b>解释：</b>唯一元素为 [1,2,3,4,5] ，和为 15 。\n",
    "</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= nums.length &lt;= 100</code></li>\n",
    "\t<li><code>1 &lt;= nums[i] &lt;= 100</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [sum-of-unique-elements](https://leetcode.cn/problems/sum-of-unique-elements/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [sum-of-unique-elements](https://leetcode.cn/problems/sum-of-unique-elements/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[1,2,3,2]', '[1,1,1,1,1]', '[1,2,3,4,5]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumOfUnique(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        sz = len(nums)\n",
    "        result = 0\n",
    "        i = 0\n",
    "        while i < sz:\n",
    "            val = nums[i]\n",
    "            cnt = 0\n",
    "            while i < sz and nums[i] == val:\n",
    "                cnt += 1\n",
    "                i += 1\n",
    "            if cnt == 1:\n",
    "                result += val\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumOfUnique(self, nums: List[int]) -> int:\n",
    "        counter = Counter(nums)\n",
    "        res = 0\n",
    "        for num in counter.keys():\n",
    "            if counter[num] == 1: res += num\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumOfUnique(self, nums: List[int]) -> int:\n",
    "        s = 0\n",
    "        for n in nums:\n",
    "            if nums.count(n) == 1:\n",
    "                s += n\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumOfUnique(self, nums: List[int]) -> int:\n",
    "        hashmap = Counter(nums)\n",
    "        suma = 0\n",
    "        for key,value in hashmap.items():\n",
    "            if value==1:\n",
    "                suma+=key\n",
    "        return suma\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumOfUnique(self, nums: List[int]) -> int:\n",
    "        hash = {}\n",
    "        target = 0\n",
    "        for i in range (len(nums)):\n",
    "            if nums[i] not in hash:\n",
    "                hash[nums[i]] = 1\n",
    "            else:\n",
    "                hash[nums[i]] += 1\n",
    "        for key in hash:\n",
    "            if hash[key] == 1:\n",
    "                target += key\n",
    "        return target"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumOfUnique(self, nums: List[int]) -> int:\n",
    "        # set_num = set(nums)\n",
    "        dic = {}\n",
    "        for i in nums:\n",
    "            if i not in dic:\n",
    "                dic[i] = i\n",
    "            else:\n",
    "                dic[i] = 0\n",
    "        \n",
    "        return sum(dic.values())\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumOfUnique(self, nums: List[int]) -> int:\n",
    "        ans = 0\n",
    "        state = [0] * 101\n",
    "        for num in nums:\n",
    "            print(state[num])\n",
    "            if  state[num] == 0:\n",
    "                ans += num\n",
    "                state[num] += 1\n",
    "            elif state[num] == 1:\n",
    "                ans -= num\n",
    "                state[num] += 1\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumOfUnique(self, nums: List[int]) -> int:\n",
    "        ad={}        \n",
    "        for i in nums:\n",
    "            ad[i]=ad.get(i,0)+1        \n",
    "        return sum([k for k in ad if ad[k]==1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "\n",
    "class Solution:\n",
    "    def sumOfUnique(self, nums: List[int]) -> int:\n",
    "        hash = [0] * 101\n",
    "        for num in nums:\n",
    "            hash[num] += 1\n",
    "        sum0 = 0\n",
    "        for i in range(101):\n",
    "            sum0 += i if hash[i] == 1 else 0\n",
    "        return sum0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumOfUnique(self, nums: List[int]) -> int:\n",
    "        unique = set()\n",
    "        repeated = set()\n",
    "        for num in nums:\n",
    "            if num not in unique:\n",
    "                if num not in repeated:\n",
    "                    unique.add(num)\n",
    "            else:\n",
    "                unique.remove(num)\n",
    "                if num not in repeated:\n",
    "                    repeated.add(num)\n",
    "        return sum(unique)\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
